package com.caterina.activities;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.util.Collection;
import java.util.GregorianCalendar;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JTextPane;

import person.Employee;
import person.Person;

import modulesAPI.v2.ViewType;
import modulesAPI.v2.composite.MTransaction;
import transaction.Transaction;
import transaction.Transaction.Type;

import com.caterina.activityManager.CActivity;
import com.caterina.components.CButton;
import com.caterina.components.COptionPane;
import com.caterina.components.CPanel;
import com.caterina.resources.CColors;
import com.caterina.resources.CModels;
import com.caterina.resources.CWords;
import controllers.ManagerTask.TaskType;

public class TransactionConfirmationActivity extends CActivity {
	public TransactionConfirmationActivity(Transaction transaction,Container parent) {
		super("TRANSACTION_CONFIRMATION",parent);
		this.transaction=transaction;
		this.status=Status.BEGIN;
	}
	
	public TransactionConfirmationActivity(Container parent) {
		super("TRANSACTION_SALARY_CONFIRMATION",parent);
		this.salaryMode=true;
		this.status=Status.BEGIN;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public void onData(TaskType task, Object data) {
		switch(task){
			case TRANSACTION_GET_WAGES_TRANSACTIONS:{
				Collection<Transaction> trs=(Collection<Transaction>) data;
				for(Transaction t:trs){
					this.salarySum+=t.getAmount();
				}
				this.transaction=new Transaction(new Person("Tutti", "i dipendenti", "", "", "", "", ""),(Employee) currentUser, Type.SALARIES_PAYMENT, salarySum, new GregorianCalendar(), "Salary payement");
				this.render();
				break;
			}
			case USER_CURRENT:{
				this.currentUser=(Employee) data;
				if(!salaryMode){
					this.transaction.setEmployee(currentUser);
					this.render();
				}
				else this.putTask(TaskType.TRANSACTION_GET_WAGES_TRANSACTIONS);
				break;
			}
			case TRANSACTION_PAY_WAGES:
			case TRANSACTION_SEND:{
				this.status=Status.OK;
				this.render();
			}
			default:break;
		}
	}

	@Override
	public void onError(TaskType task, Exception e) {
		this.errorMsg=CWords.getWord(e.getMessage());
		this.status=Status.ERROR;
		this.render();
	}
	
	
	@Override
	public void onStart() {
		this.putTask(TaskType.USER_CURRENT);
	}
	private void render(){
		CPanel mainPanel=new CPanel(new BorderLayout());
		mainPanel.add(getMessagePanel(),BorderLayout.NORTH);
		mainPanel.add(this.getTransactionsPanel(),BorderLayout.CENTER);
		mainPanel.add(getButtonsPanel(),BorderLayout.SOUTH);
		this.add(mainPanel);
	}
	private CPanel getMessagePanel(){
		CPanel retPanel=new CPanel(new BorderLayout());
		JTextPane txt=new JTextPane();
		txt.setMinimumSize(new Dimension(200,200));
		String color="black";
		switch(status){
			case OK:{txt.setBackground(CColors.GREEN);color="white";break;}
			case BEGIN:{txt.setBackground(CColors.WHITE);color="black";break;}
			case ERROR:{txt.setBackground(CColors.RED);color="white";break;}
		}
		if(status!=Status.ERROR)errorMsg="";
		txt.setContentType("text/html");
		txt.setText("<html><head>"+CModels.getCaterinaStyle()+"<style type=\"text/css\">body{ color: "+color+";}</style></head><body>"+CWords.getWord("TRANSACTION_MESSAGE_"+status.name())+" "+errorMsg+"</body></html>");
		retPanel.add(txt,BorderLayout.CENTER);
		retPanel.setMinimumSize(new Dimension(100,100));
		return retPanel;
	}
	private CPanel getButtonsPanel(){
		CPanel retPanel =new CPanel();
		retPanel.setLayout(new BoxLayout(retPanel, BoxLayout.LINE_AXIS));
		retPanel.add(Box.createHorizontalGlue());
		if (this.status!=Status.OK){
			CButton sendButton=new CButton(CWords.getWord("SEND_TRANSACTION")) {
				@Override
				public void onFocus() {}
				@Override
				public void onAction() {
					startLoading();
					if(salaryMode) putTask(TaskType.TRANSACTION_PAY_WAGES);
					else putTask(TaskType.TRANSACTION_SEND, transaction);
				}
				private static final long serialVersionUID = 1L;
			};
			sendButton.setPreferredSize(new Dimension(200,30));
			sendButton.setMaximumSize(new Dimension(200,30));
			sendButton.setMinimumSize(new Dimension(200,30));
			retPanel.add(sendButton);
		}
		String cancelText="CANCEL_TRANSACTION";
		if (this.status==Status.OK)cancelText=COptionPane.OK;
		CButton cancelButton=new CButton(CWords.getWord(cancelText)) {
			@Override
			public void onFocus() {}
			@Override
			public void onAction() {
				close(this.getName());
			}
			private static final long serialVersionUID = 1L;
		};
		cancelButton.setName(cancelText);
		cancelButton.setPreferredSize(new Dimension(200,30));
		cancelButton.setMaximumSize(new Dimension(200,30));
		cancelButton.setMinimumSize(new Dimension(200,30));
		retPanel.add(Box.createHorizontalStrut(20));
		retPanel.add(cancelButton);
		return retPanel;
	}
	private CPanel getTransactionsPanel(){
		CPanel TransactionsPanel=new CPanel();
		TransactionsPanel.setLayout(new BoxLayout(TransactionsPanel, BoxLayout.PAGE_AXIS));
		MTransaction transactionPanel=new MTransaction(null,transaction, "TRANSACTION", "TRANSACTION_DESC", ViewType.IN_LIST);
		TransactionsPanel.add(Box.createVerticalStrut(10));
		CPanel inTrPanel=transactionPanel.getInListPanel(false);
		inTrPanel.setMaximumSize(new Dimension(2000,80));
		TransactionsPanel.add(inTrPanel);
		TransactionsPanel.add(Box.createVerticalStrut(10));
		return TransactionsPanel;
	}
	private Employee currentUser;
	private boolean salaryMode=false;
	private Transaction transaction;
	private double salarySum=0;
	public enum Status{OK,BEGIN,ERROR};
	private String errorMsg;
	private Status status;
	private static final long serialVersionUID = 1L;
}
